package com.hsoft.zhxy.app.xtgl_02_01.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsoft.zhxy.app.common.model.entity.JcqxBm;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcqxBmMapper;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcqxYhbmMapper;
import com.hsoft.zhxy.app.xtgl_02_01.model.query.BmxxQUERY;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.BmtreeVO;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.JcqxBmDrVO;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.JcqxBmListVO;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.YhbmxxVO;
import com.hsoft.zhxy.app.xtgl_02_01.service.JcqxBmService;
import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import com.hsoft.zhxy.common.util.UUIDUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

 /**
 * All rights Reserved, Designed By 哈尔滨工业大学软件工程股份有限公司
 * 
 * @Title: 部门 Service层接口实现类
 * @Package 
 * @Description: 
 * @author: system
 * @date: 2020-10-26
 */
@Log4j2
@Service
public class JcqxBmServiceImpl extends BaseService implements JcqxBmService {

    @Autowired
    private JcqxBmMapper jcqxBmMapper;
    @Autowired
    private JcqxYhbmMapper jcqxYhbmMapper;


    /**
     * 查询单条数据
     * @param jcqxBm
     * @return
     */
    public JSONObject findBmByBmm(JcqxBm jcqxBm) {
        JcqxBm rs = jcqxBmMapper.selectOne(new QueryWrapper<JcqxBm>().setEntity(jcqxBm));
        return ResultBody.setData(rs);
    }


    /**
     * 查询部门树列表数据
     * @param jcqxBm
     * @return
     */
    public JSONObject findBmList(JcqxBm jcqxBm) {
        List<BmtreeVO> list = jcqxBmMapper.selecBmtList(new QueryWrapper<JcqxBm>().setEntity(jcqxBm));
        //获取根节点树
        List<BmtreeVO> rootList= new ArrayList<BmtreeVO>();
        Iterator<BmtreeVO> it=list.iterator();
        while(it.hasNext()){
            BmtreeVO tmp=it.next();
            if(StringUtils.isBlank(tmp.getFbmm())||tmp.getBmm().equals(tmp.getFbmm())){
                rootList.add(tmp);
            }
        }
//        循环生成树
        Iterator<BmtreeVO> rootIt=rootList.iterator();
        while(rootIt.hasNext()){
            BmtreeVO tmp=rootIt.next();
            //递归生成树
            tmp=buildChilTree(tmp,list);
        }

//        log.debug(rootList.toString());
        return ResultBody.setData(rootList);
    }


     //递归，建立子树形结构
     private BmtreeVO buildChilTree(BmtreeVO bm,List<BmtreeVO> list){
          List<BmtreeVO> chil =new  ArrayList<BmtreeVO>();
          for(BmtreeVO bmtreeVO : list) {
              if(StringUtils.isNotBlank(bmtreeVO.getFbmm())&&bmtreeVO.getFbmm().equals(bm.getBmm())) {
                  chil.add(buildChilTree(bmtreeVO,list));
              }
          }
          bm.setChildren(chil);
          return bm;
      }


    /**
     * 查询分页数据
     * @param bmxxQUERY
     * @return
     */
    public JSONObject findBmListByPageFbmm(BmxxQUERY bmxxQUERY) {

        Page<JcqxBm> pageObj = new Page<JcqxBm>(bmxxQUERY.getPage(), bmxxQUERY.getPageSize());
        QueryWrapper<BmxxQUERY> queryWrapper= new QueryWrapper<BmxxQUERY>();
        queryWrapper.eq("fbmm" , bmxxQUERY.getBmm())
                .orderByAsc("pxh");
        IPage<JcqxBm> result = jcqxBmMapper.selectBmListByPageFbmm(pageObj, queryWrapper);

        return ResultBody.setData(result.getTotal(), result.getRecords());
    }
     /**
      * 通过部门码查询所属用户
      * @param bmxxQuery
      * @return
      */
     @Override
     public JSONObject findYhListByPageBmm(BmxxQUERY bmxxQuery) {
         Page<YhbmxxVO> pageObj = new Page<YhbmxxVO>(bmxxQuery.getPage(), bmxxQuery.getPageSize());
         QueryWrapper<BmxxQUERY> queryWrapper= new QueryWrapper<BmxxQUERY>();
         queryWrapper.eq("b.bmm" , bmxxQuery.getBmm())
                 .eq("a.yhzt",1)
                 .orderByAsc("c.pxh");
//                 .orderByAsc("b.pxh");
         IPage<YhbmxxVO> result = jcqxBmMapper.findYhListByPageBmm(pageObj, queryWrapper);
         return ResultBody.setData(result.getTotal(), result.getRecords());
     }


     @Override
     public JSONObject findYhListByBmm(JcqxBm jcqxBm) {
         QueryWrapper<JcqxBm> queryWrapper= new QueryWrapper<JcqxBm>();
         queryWrapper.eq("b.bmm" , jcqxBm.getBmm())
                 .eq("a.yhzt",1)
                 .orderByAsc("c.pxh");
//                 .orderByAsc("b.pxh");
         List<YhbmxxVO> result = jcqxBmMapper.findYhListByBmm(queryWrapper);
         return ResultBody.setData(result);
     }

     @Override
     public JSONObject drBmBatch(JcqxBmDrVO jcqxBmDrVO) {
        List<YhbmxxVO> yh=jcqxBmDrVO.getYhlist();
        List<String> yhids=new ArrayList<String>();
        List<String> ids=new ArrayList<String>();
        yh.forEach(item->{
            yhids.add(item.getYhid());
        });

        int sfzz=1;
        if(jcqxBmDrVO.getSfzzdr()==1&&yhids.size()>0){
            //主职调入

            sfzz=0;
            QueryWrapper updateQueryWrapper = new QueryWrapper();
            //更新list中用户所有状态为兼职状态
            updateQueryWrapper.in("yhid",yhids);
            jcqxBmMapper.updateYhbmzt(updateQueryWrapper);
        }
//        查询部门下的用户部门信息
         QueryWrapper<JcqxBm> yhbmQueryWrapper= new QueryWrapper<JcqxBm>();
         yhbmQueryWrapper.eq("bmm" ,jcqxBmDrVO.getBmm() )
                 .in("yhid",yhids);
         //更新部门下的用户部门状态
         List<YhbmxxVO> yhbmIdList=jcqxBmMapper.findYhListByYhidBmm(yhbmQueryWrapper);
         QueryWrapper<JcqxBm> updateQueryWrapper = new QueryWrapper();
         yhbmIdList.forEach(item->{
             ids.add(item.getId());
         });
         if(ids.size()>0){
             updateQueryWrapper.eq("bmm",jcqxBmDrVO.getBmm()).in("id",ids);
             jcqxBmMapper.updateYhbmBatch(jcqxBmDrVO.getSfzzdr(),updateQueryWrapper);
         }

        //循环获取需要插入的用户部门状态
         Iterator<YhbmxxVO> it=yh.iterator();
        while (it.hasNext()){
            YhbmxxVO t= it.next();
            t.setId(UUIDUtils.getUUID());
            boolean flag=false;
            for(YhbmxxVO yhbmxxVO:yhbmIdList){
                if(t.getYhid().equals(yhbmxxVO.getYhid())&&t.getBmm().equals(yhbmxxVO.getBmm())){
                    flag=false;
                    break;
                }
            }
            if(flag){
                it.remove();
            }
        }
         //插入未在部门下的用户部门状态
         return ResultBody.setData(jcqxBmMapper.insertYhbmBatch(yh,jcqxBmDrVO.getSfzzdr(),jcqxBmDrVO.getBmm()));


     }


     /**
     * 新增
     * @param jcqxBm
     * @return
     */
    public JSONObject addBm(JcqxBm jcqxBm) {
        jcqxBm.setCjsj(LocalDateTime.now());
        jcqxBm.setCjr(getYhid());
        jcqxBm.setBgsj(LocalDateTime.now());
        jcqxBm.setBgr(getYhid());
        return ResultBody.setData(jcqxBmMapper.insert(jcqxBm));
    }


    /**
     * 修改
     * @param jcqxBm
     * @return
     */
    public JSONObject update(JcqxBm jcqxBm) {
        jcqxBm.setBgsj(LocalDateTime.now());
        jcqxBm.setBgr(getYhid());
        return ResultBody.setData(jcqxBmMapper.updateById(jcqxBm));
    }


    /**
     * 删除
     * @param jcqxBmListVO
     * @return
     */
    @Override
    public JSONObject deleteBatch(JcqxBmListVO jcqxBmListVO) {
        List<JcqxBm> bmmList=jcqxBmListVO.getJcqxBmList();
        List<String> ids=new ArrayList<String>();
        Iterator<JcqxBm> it=bmmList.iterator();
        while (it.hasNext()){
            ids.add(it.next().getBmm());
        }

        QueryWrapper<List<String>> queryWrapper= new QueryWrapper<List<String>>();
        queryWrapper.in("bmm",ids);
        return ResultBody.setData(jcqxBmMapper.deleteBatch(queryWrapper));
    }






 }
